import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author hht
 * @ClassName LambdaTester
 * @Description TODO
 * @Date 2019/2/19 14:20
 * @VERSION 1.0
 */
public class LambdaTester {

    public static void main(String[] args) {

//        List<String> lists=Arrays.asList("a","b","c","d");
//        for (String s:lists){
//            System.out.println(s);
//        }
//        //1.只能顺序处理List中的元素（process one by one）
//        //2.不能充分利用多核CPU
//        //3.不利于编译器优化
//
//        List<String> lists2=Arrays.asList("a","b","c","d");
//        lists2.forEach(i-> System.out.println(i));
//        String s = filter(lists2,str->str.equals("a"));


        //1.不一定需要顺序处理List中的元素，顺序可以不确定
        //2.可以并行处理，充分利用多核CPU的优势
        //3.有利于JIT编译器对代码进行优化
//
//
//        //创建线程的方式
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("jdk 1.7 apply");
//            }
//        }).start();
//        final  int  i = 5;//直接在 lambda中使用的变量不可变
//        new Thread(()->{
//            System.out.println("jdk 1.8 test"+i);
//        }).start();
//        new Thread(()-> System.out.println(i)).start();
//
//        //函数式接口调用
//        int value=10;
//        int result1=changeTheValue(value,j->j+5);
//        System.out.println("result1:"+result1);
//        int result2=changeTheValue(value,(j)->j*30);
//        System.out.println("result2:"+result2);

        // list 转 Set
        List<Man> manList = new ArrayList<>();
        Man man = new Man();
        man.setId("1");
        man.setName("人");
        manList.add(man);

        Set<Man> manSet =  manList.stream().collect(Collectors.toSet());
        //某个属性转 set
//        manSet =  manList.stream().map(w->Man::getId).collect(Collectors.toSet());

        manSet.contains("1");
        Iterator<Man> iterable =  manSet.iterator();
        while (iterable.hasNext()){
            System.out.println(iterable.next().getId());
        }


        //list 转map
//        Map m = lists2.stream().collect(Collectors.toMap(w->w.hashCode(),w->w));
//        System.out.println(m);
//
//        //Runn
//        Runnable r = ()-> System.out.println(1);
//        new Thread(r).start();
//        System.out.println(2);
//        Page page = new LambdaTester().new Page(1);
//        page.add(1);
//        page.add(2);
//        System.out.println(page.getA());
//        List list = (Page) page;
//        Page page2 = (Page)list;
//        System.out.println(page2.getA());


        //对list分组
//        List<Person> list = getList();
//        //按姓进行分组
//        Map<String, List<Person>> collect = list.stream().collect(Collectors.groupingBy(p -> p.name.substring(0, 1), Collectors.toList()));
//        System.out.println(collect);

    }

    //函数式接口实现
    @FunctionalInterface
    public  interface  Function<T,R>{
        R apply(T t);
    }


    public static int changeTheValue(int value ,Function<Integer,Integer> function){
        return function.apply(value);
    }

    public static String filter(List<String> names, Predicate condition) {
        for(String name: names)  {
            if(condition.test(name)) {
                System.out.println(name + "===== ");
            }
        }
        return  null;
    }

    class Page<E> extends ArrayList<E> implements Closeable {
        public Integer a ;

        public Integer getA() {
            return a;
        }

        public void setA(Integer a) {
            this.a = a;
        }

        public Page(Integer a){
            this.a = a;
        }

        @Override
        public void close() throws IOException {

        }
    }

}
class Man{
    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}